<?php
namespace app\admin\controller;
use app\admin\model\myshuju\MyHandle;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Stesystem extends BaseController
{
    public function show_list(Request $request){
        $en_name = $request->param('en_name');
        $data = Db::name(getTable('systems'))
            ->where('en_name',$en_name)
            ->where('status',1)
            ->order('sort desc')
            ->select()->toArray();
        $form = Builder::form()->setAction($request->controller().'/edit');
//        '单行文本',
//                '多行文本',
//                '单文件',
//                '多文件',
//                '单图片',
//                '多图片',
//                '视频',
//                '单选框',
//                '多选框',
//                '下拉框',
//                '编辑框',
//                '编辑框(layui)',
//                '开关',
//                '时间',
//                'json数组'
        foreach ($data as $value){
            if($value['tupian']=='单行文本'){
                $form->setProject('input',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='多行文本'){
                $form->setProject('textarea',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='单文件'){
                $form->setProject('file',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='多文件'){
                $form->setProject('files',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='单图片'){
                $form->setProject('img',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='多图片'){
                $form->setProject('imgs',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='视频'){
                $form->setProject('video',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='单选框'){
                if($value['option']){
                    $array = explode(',',$value['option']);
                }else{
                    $array = [];
                }
                $form->setProject('radio',$value['name'],$value['name'],$value['value'],$array,$value['descs']);
            }
            if($value['tupian']=='多选框'){
                if($value['option']){
                    $array = explode(',',$value['option']);
                }else{
                    $array = [];
                }
                $form->setProject('checkbox',$value['name'],$value['name'],$value['value'],$array,$value['descs']);
            }
            if($value['tupian']=='下拉框'){
                if($value['option']){
                    $array = explode(',',$value['option']);
                }else{
                    $array = [];
                }
                $form->setProject('video',$value['name'],$value['name'],$value['value'],$array,$value['descs']);
            }
            if($value['tupian']=='编辑框'){
                $form->setProject('editor',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='编辑框(layui)'){
                $form->setProject('editors',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='开关'){
                $form->setProject('switch',$value['name'],$value['name'],$value['value'],['开启','关闭'],$value['descs']);
            }
            if($value['tupian']=='时间'){
                $form->setProject('time',$value['name'],$value['name'],$value['value'],[],$value['descs']);
            }
            if($value['tupian']=='json数组'){
                if($value['option']){
                    $array = explode(',',$value['option']);
                }else{
                    $array = ['',''];
                }
                $form->setProject('json',$value['name'],$value['name'],$value['value'],$array,$value['descs']);
            }
        }
        return $form->setButton('submit','提交修改')
            ->setButton('reload','刷新')
            ->fetch();
    }
    public function edit(Request $request){
        $data = $request->param();
        $obj = MyHandle::GetConfig();
        $data = $obj->guolu($data,['/admin/stesystem/edit_html']);
        foreach ($data as $key=>$value){
            $jiu = Db::name(getTable('systems'))
                ->where('name',$key)
                ->find();
            if($jiu['tupian']=='开关'){
                if($value=='on'){
                    $value = 1;
                }else{
                    $value = 0;
                }
            }
            if($jiu['tupian']=='json数组'){
                $value = json_encode($value,JSON_UNESCAPED_UNICODE);
            }
            Db::name(getTable('systems'))
                ->where('id',$jiu['id'])
                ->update(['value'=>$value]);
        }
        return $this->success('修改成功',200,'',url('stesystem/show_list?en_name='.$jiu['en_name']));
    }
}